﻿
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using System.Threading;

using Gift.DAL;
using Gift.Model;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;

public partial class admin_system_roleList : BaseAdmin
{


    protected string kw = "";
    protected string order = "roleName";
    protected int ot = 0;
    protected int pageCount = 0;
    protected int pageIndex = 1;
    protected int pageSize = 20;
    protected string parentType = "";
    protected string pid = "";
    protected PageList pl = new PageList();
    protected int t = 0;
    protected int total = 0;

    bool _isEdit = false;
    bool _isDeleted = false;
    bool _isPermission = false;
    string _addDisplay = "none";

    public string AddDisplay
    {
        get { return _addDisplay; }
    }

    // Methods
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {

            string str = base.Request.Form["Item"];

            BaseFactory.theRole.DeleteList(str);
            BaseFactory.theRoleRuleRef.DeleteListByRoleId(str);

        }
        catch
        {
        }

        base.Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);
    }

    private void RoleBind()
    {
        try
        {
            if (base.Request.QueryString["p"] != null)
            {
                this.pageIndex = int.Parse(base.Request.QueryString["p"]);
            }
            if (base.Request.QueryString["ps"] != null)
            {
                this.pageSize = Convert.ToInt32(base.Request.QueryString["ps"]);
            }
            if (base.Request.QueryString["or"] != null)
            {
                this.order = base.Request.QueryString["or"];
            }
            if (base.Request.QueryString["ot"] != null)
            {
                this.ot = Convert.ToInt32(base.Request.QueryString["ot"]);
            }
            if (base.Request.QueryString["pid"] != null)
            {
                this.pid = base.Request.QueryString["pid"];
            }
            this.pl.TblName = "tbRole";
            this.pl.StrGetFields = "id,RoleName,IsDeleted,Remark,convert(nvarchar(10),createTime,121) as createTime";
            this.pl.FldName = this.order;
            this.pl.PageSize = this.pageSize;
            this.pl.PageIndex = this.pageIndex;
            this.pl.DoCount = 1;
            this.pl.OrderType = this.ot;
            this.pl.StrWhere = " isnull(isDeleted,0) = 0 and companyId = " + base.Administrator.CompanyId;
            if ((base.Request.QueryString["t"] != null) && (base.Request.QueryString["kw"] != null))
            {
                this.kw = base.Request.QueryString["kw"];
                this.pl.StrWhere =  this.pl.StrWhere  + " and " + base.Request.QueryString["t"] + " like '%" + this.kw + "%' ";
            }

            this.total = Convert.ToInt32(BaseFactory.thePageList.GetPageList(this.pl).Rows[0]["Total"]);
            this.pl.DoCount = 0;
            DataTable pageList = BaseFactory.thePageList.GetPageList(this.pl);
            new PageListNo().DelPageSize(pageList, this.total);
            if (pageList.Rows.Count == 0)
            {
                this.btnDelete.Visible = false;
            }
            this.rptRole.DataSource = pageList.DefaultView;
            this.rptRole.DataBind();
        }
        catch(Exception ex)
        {
        }
    }

    void DeleteItem()
    {
        if (Request.QueryString["action"] == "del" && !string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            string str = Request.QueryString["id"];
            BaseFactory.theRole.DeleteList(str);
            BaseFactory.theRoleRuleRef.DeleteListByRoleId(str);
            base.ClientScript.RegisterStartupScript(base.GetType(), "sd", "<script language=\"javascript\" defer>alert('删除成功！');</script>");
        }
    }



    protected override object LoadPageStateFromPersistenceMedium()
    {
        byte[] inArray = ViewStateCompression.DeCompress(Convert.FromBase64String(base.Request.Form.Get("__MSPVSTATE")));
        LosFormatter formatter = new LosFormatter();
        return formatter.Deserialize(Convert.ToBase64String(inArray));
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!base.IsPostBack)
        {
            this.ValidateLoginUser();
            this.DeleteItem();
            if (base.Request.QueryString["t"] != null)
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "sT", "<script type=\"text/javascript\">selectIsExitItem(\"selectType\",\"" + base.Request.QueryString["t"] + "\");</script>");
            }
            if (base.Request.QueryString["ps"] != null)
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "sPs", "<script type=\"text/javascript\">selectIsExitItem(\"selectPageSize\",\"" + base.Request.QueryString["ps"] + "\");</script>");
            }
         
            this.btnDelete.Attributes.Add("disabled", "disabled");
            this.RoleBind();
        }
    }


    protected void rptRole_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        base.SetListPerssion(e, _isEdit, _isDeleted,_isPermission);

    }

    void ValidateLoginUser()
    {
        if (!base.GetValidateLoginRule(Gift.Model.ENUM.ERule.RoleList))
        {
            base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
            base.Response.End();
        }
        if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.RoleAdd))
        {
            _addDisplay = "block";
        }
        if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.RoleEdit))
        {
            _isEdit = true;
        }
        if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.RoleDeleted))
        {
            _isDeleted = true;
        }
        if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.ManagerRole))
        {
            _isPermission = true;
        }

    }



    protected override void SavePageStateToPersistenceMedium(object pViewState)
    {
        LosFormatter formatter = new LosFormatter();
        StringWriter output = new StringWriter();
        formatter.Serialize(output, pViewState);
        string hiddenFieldInitialValue = Convert.ToBase64String(ViewStateCompression.Compress(Convert.FromBase64String(output.ToString())));
        base.ClientScript.RegisterHiddenField("__MSPVSTATE", hiddenFieldInitialValue);
    }

}
